﻿/*****************************************介绍*****************************************
 * 作者：Ywh
 * 创建时间：2024-05-25 14:06:29
 * 功能：管理器基类（普通类的单例）
**************************************************************************************/

using Game.Framework;
using System;
using System.Reflection;

public abstract class BaseManager<T> where T : class
{
    private static T m_Instance;
    protected static readonly object m_LockObj = new object();
    protected bool InstanceisNull => m_Instance == null;

    public static T Instance
    {
        get
        {
            if (m_Instance == null)
            {
                lock (m_LockObj)
                {
                    if (m_Instance == null)
                    {
                        Type type = typeof(T);
                        ConstructorInfo info = type.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic,
                                                                   null,
                                                                   Type.EmptyTypes,
                                                                   null);
                        if (info != null)
                            m_Instance = info.Invoke(null) as T;
                        else
                            LogManager.LogError($"缺少私有无参构造函数，type:{type.Name}", "BaseManager");
                    }
                }
            }
            return m_Instance;
        }
    }
}